package com.sa07.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.sa07.exception.ConfigInitFailedException;

/**
 * propertie配置文件初始化
 * 
 * @author Long.shangbo
 *
 * 2014-2-21 上午10:29:03
 */
public abstract class PropertiesConfigInit implements IConfigInit {

	protected static final Logger logger = Logger.getLogger(PropertiesConfigInit.class);
	
	@Override
	public void init() throws ConfigInitFailedException {
		Properties pro = new Properties();
		InputStream in = null;
		
		try {
			in = getConfigInputStream();
			pro.load(in);
			analyzeProperties(pro);
		} catch (Exception e) {
			logger.error("Init properties failed, caused by -> " + e);
			throw new ConfigInitFailedException(e);
		} finally{
			if(in != null){
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 获得配置文件
	 * @return
	 */
	protected abstract InputStream getConfigInputStream() throws Exception;
	
	/**
	 * 解析Properties对象
	 * @param doc
	 * @return 
	 */
	protected abstract void analyzeProperties(Properties pro);
}
